package com.jdrx.sw.sewage.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.vo.ExcelDevStorageVO;
import com.jdrx.sw.sewage.beans.vo.MaterielVO;
import com.jdrx.sw.sewage.common.converter.LocalDateConverter;
import com.jdrx.sw.sewage.common.util.SpringBeanUtil;
import com.jdrx.sw.sewage.dao.DevStorageDAO;
import com.jdrx.sw.sewage.dao.MaterielDAO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.jdrx.sw.sewage.common.constant.Constant.ERR_MESSAGE_CODE;

@Service
public class DevStorageImportCheckServiceImpl extends AnalysisEventListener<ExcelDevStorageVO> {

    public static final Logger logger = LoggerFactory.getLogger(DevStorageImportCheckServiceImpl.class);
    List<ExcelDevStorageVO> list = new ArrayList();
    Map map = Maps.newHashMap();
    Map snMap = Maps.newHashMap();
    Integer row = 1;

    @Autowired
    private MaterielDAO materielDAO;
    @Autowired
    private DevStorageDAO devStorageDAO;

    @Override
    public void invoke(ExcelDevStorageVO data, AnalysisContext context) {
        logger.info("解析到一条数据:{}", JSON.toJSONString(data));
        String materielName = data.getMaterielName();
        MaterielVO materielVO = materielDAO.selectByName(materielName);
        List li = Lists.newArrayList();
        if (StringUtils.isEmpty(materielName)) li.add("第" + row + "行第1列" + "物料类型为空");
        if (Objects.isNull(materielVO)) li.add("第" + row + "行第1列" + "物料类型不存在");
        if (StringUtils.isEmpty(data.getCategoryName())) li.add("第" + row + "行第2列" + "设备品类为空");
        if (StringUtils.isEmpty(data.getFactoryName())) li.add("第" + row + "行第3列" + "生产厂家为空");
        if (null == data.getPurchaseTime()) li.add("第" + row + "行第4列" + "采购时间为空");
        if (null == data.getMaintenanceCycleDay()) li.add("第" + row + "行第5列" + "维保周期固定周期为空");
        if (null == data.getMaintenanceCycleHour()) li.add("第" + row + "行第6列" + "维保周期工作小时为空");
        if (null == data.getDossierSpec()) li.add("第" + row + "行第7列" + "设备规格为空");
        if (null == data.getStorageNum()) li.add("第" + row + "行第8列" + "入库数量为空");
        if (StringUtils.isNotEmpty(data.getSnNum())) {
            if (1 < data.getStorageNum()) li.add("第" + row + "行第9列" + "设备SN号不为空时，入库数量必须为1");
            Integer count = devStorageDAO.countDevBYStorageNum(data.getSnNum());
            if (count >= 1) li.add("第" + row + "行第9列" + "设备SN号和数据库重复");

            if (StringUtils.isEmpty((String) snMap.get(data.getSnNum())))
                snMap.put(data.getSnNum(), data.getSnNum());
            else li.add("第" + row + "行第9列" + "表格中设备SN号重复");
        }

        if (li.size() > 0) {
            data.setMsg(li);
            list.add(data);
        }
        row++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        map.put("msgData", list);
        map.put("total", context.getTotalCount() - 1);
        row = 1;
        logger.info("所有数据校验完成！");
    }

    public ResponseVO deviceStorageImportCheck(MultipartFile file) throws IOException {
        snMap.clear();
        list.clear();
        map.clear();
        EasyExcel.read(file.getInputStream(), ExcelDevStorageVO.class, SpringBeanUtil.getBean(DevStorageImportCheckServiceImpl.class))
                .registerConverter(new LocalDateConverter())
                .sheet()
                .doRead();

        if (list.size() > 0) return ResponseFactory2.err(map, ERR_MESSAGE_CODE, "校验不通过");
        return ResponseFactory2.ok(map);
    }
}